<template>
  <div ref="chartDiv" :style="currStyle"></div>
</template>

<script setup>
import {ref, watch, onMounted, nextTick, markRaw} from 'vue';
import * as echarts from "echarts";
import visual from "@/visual/js/visual";
import * as chart from "@/visual/js/echarts";
import JSON5 from 'json5';
import { parseColors } from "@/utils/color";

const props = defineProps({
  node: Object,
  compStyle: Object,
  compData: Object
});
const currNode = ref(props.node);
const chartDiv = ref();
const currChart = ref(null);
const currStyle = ref();

const init = async() => {
  if (currNode.value.compData.dataSourceType === '') {
    currNode.value.compData.dataSourceType = '静态数据';
  }
  const compStyle = props.compStyle || {};
  let style = '';
  style += visual.parseTpl('width: {value};', compStyle['width']);
  style += visual.parseTpl('height: {value};', compStyle['height']);
  currStyle.value = style;

  const padding = (compStyle['padding'] || '50px,20px,30px,20px').split(',');
  let defaultOption = {};
  try {
    defaultOption = JSON5.parse(compStyle['jsonOption'] || '{}');
  } catch (e) {
    console.log("jsonOption解释出错", e)
  }
  const dataList = (await visual.loadData(props.compData, {}, {}, currNode.value)).data || [];
  currNode.value.compData.dataList = JSON.stringify(dataList);

  let labelData = [];
  const legendData = [];
  const seriesData = [];
  (compStyle.seriesList || []).forEach(item => {
    const fieldData = [];
    for (let i=0; i<dataList.length; i++) {
      fieldData[i] = dataList[i][item.prop];
    }

    if (item.dataType === 'label') {
      labelData = fieldData;
    } else {
      legendData.push(item.name);
      seriesData.push({
        type: item.type,
        name: item.name,
        data: fieldData,
        stack: item.stack ? (item.stackName || 'Total') : null,
        ...chart.parseSeries(item)
      });
    }
  });

  const option = {
    animation: compStyle['animation'] || false,
    color: parseColors(compStyle['colorScheme']),
    title: chart.parseTitle(compStyle),
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: chart.parseLegend(compStyle, legendData),
    grid: {
      top: padding[0],
      right: padding[1],
      bottom: padding[2],
      left: padding[3],
      containLabel: true
    },
    xAxis: chart.parseAxis('xAxis', compStyle, [labelData]),
    yAxis: chart.parseAxis('yAxis', compStyle, [labelData]),
    series: seriesData,
    toolbox: chart.parseToolbox(compStyle)
  };

  await nextTick(() => {
    try {
      if (currChart.value) {
        echarts.dispose(chartDiv.value);
      }
      currChart.value = markRaw(echarts.init(chartDiv.value));
      currChart.value.setOption(Object.assign({}, option, defaultOption));
    } catch (e) {
      console.log(e);
    }
  });
  await nextTick(() => currChart.value.resize());
};

watch(() => props.compStyle, () => {
  init();
}, { deep: true });

watch(() => props.compData?.refresh, () => {
  init();
});

onMounted(() => {
  init();
});
</script>